
//=============================================================================
// Sprite_Animation
//=============================================================================

Sprite_Animation.prototype.setupRate = function () {
    this._rate = Yanfly.Param.AnimationRate;
};

//=============================================================================
// Sprite_Battler
//=============================================================================

if (!Yanfly.Param.FlashTarget) {

    Yanfly.Core.Sprite_Battler_updateSelectionEffect =
        Sprite_Battler.prototype.updateSelectionEffect;
    Sprite_Battler.prototype.updateSelectionEffect = function () {
        if (this._battler.isActor()) {
            Yanfly.Core.Sprite_Battler_updateSelectionEffect.call(this);
        } else {
            if (this._battler.isSelected()) this.startEffect('whiten');
        }
    };

}; // Yanfly.Param.FlashTarget

//=============================================================================
// Sprite_Actor
//=============================================================================

if (Yanfly.Param.ReposBattlers) {
    Yanfly.Core.Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
    Sprite_Actor.prototype.setActorHome = function (index) {
        Yanfly.Core.Sprite_Actor_setActorHome.call(this, index);
        this._homeX += Graphics.boxWidth - 816;
        this._homeY += Graphics.boxHeight - 624;
    };
};

Sprite_Actor.prototype.retreat = function () {
    this.startMove(1200, 0, 120);
};

//=============================================================================
// Sprite_Enemy
//=============================================================================

if (Yanfly.Param.ReposBattlers) {

    Yanfly.Core.Sprite_Enemy_setBattler = Sprite_Enemy.prototype.setBattler;
    Sprite_Enemy.prototype.setBattler = function (battler) {
        Yanfly.Core.Sprite_Enemy_setBattler.call(this, battler);
        if (!this._enemy._alteredScreenY) {
            this._homeY += Math.floor((Graphics.boxHeight - 624) / 2);
            this._enemy._screenY = this._homeY;
            this._enemy._alteredScreenY = true;
        }
        if ($gameSystem.isSideView()) return;
        if (!this._enemy._alteredScreenX) {
            this._homeX += (Graphics.boxWidth - 816) / 2;
            this._enemy._screenX = this._homeX;
            this._enemy._alteredScreenX = true;
        }
    };

}; // Yanfly.Param.ReposBattlers

//=============================================================================
// Sprite_StateIcon
//=============================================================================

Sprite_StateIcon._iconWidth = Yanfly.Param.IconWidth;
Sprite_StateIcon._iconHeight = Yanfly.Param.IconHeight;

//=============================================================================
// Sprite_Button
//=============================================================================

Sprite_Button.prototype.isButtonTouched = function () {
    var x = this.canvasToLocalX(TouchInput.x) + (this.anchor.x * this.width);
    var y = this.canvasToLocalY(TouchInput.y) + (this.anchor.y * this.height);
    return x >= 0 && y >= 0 && x < this.width && y < this.height;
};

//=============================================================================
// Sprite_Battleback
//=============================================================================

function Sprite_Battleback() {
    this.initialize.apply(this, arguments);
}

Sprite_Battleback.prototype = Object.create(Sprite.prototype);
Sprite_Battleback.prototype.constructor = Sprite_Battleback;

Sprite_Battleback.prototype.initialize = function (bitmapName, type) {
    Sprite.prototype.initialize.call(this);
    this._bitmapName = bitmapName;
    this._battlebackType = type;
    this.createBitmap();
};

Sprite_Battleback.prototype.createBitmap = function () {
    if (this._bitmapName === '') {
        this.bitmap = new Bitmap(Graphics.boxWidth, Graphics.boxHeight);
    } else {
        if (this._battlebackType === 1) {
            this.bitmap = ImageManager.loadBattleback1(this._bitmapName);
        } else {
            this.bitmap = ImageManager.loadBattleback2(this._bitmapName);
        }
        this.scaleSprite();
    }
};

Sprite_Battleback.prototype.scaleSprite = function () {
    if (this.bitmap.width <= 0) return setTimeout(this.scaleSprite.bind(this), 5);
    var width = Graphics.boxWidth;
    var height = Graphics.boxHeight;
    if (this.bitmap.width < width) {
        this.scale.x = width / this.bitmap.width;
    }
    if (this.bitmap.height < height) {
        this.scale.y = height / this.bitmap.height;
    }
    this.anchor.x = 0.5;
    this.x = Graphics.boxWidth / 2;
    if ($gameSystem.isSideView()) {
        this.anchor.y = 1;
        this.y = Graphics.boxHeight;
    } else {
        this.anchor.y = 0.5;
        this.y = Graphics.boxHeight / 2;
    }
};

//=============================================================================
// Sprite_Character
//=============================================================================

Yanfly.Core.Sprite_Character_updateHalfBodySprites =
    Sprite_Character.prototype.updateHalfBodySprites;
Sprite_Character.prototype.updateHalfBodySprites = function () {
    Yanfly.Core.Sprite_Character_updateHalfBodySprites.call(this);
    if (this._bushDepth > 0) {
        this._upperBody.blendMode = this.blendMode;
        this._lowerBody.blendMode = this.blendMode;
    }
};

//=============================================================================
// Spriteset_Map
//=============================================================================

Spriteset_Map.prototype.hideCharacters = function () {
    for (var i = 0; i < this._characterSprites.length; i++) {
        var sprite = this._characterSprites[i];
        if (!sprite.isTile()) sprite.hide();
    }
};

Spriteset_Map.prototype.showCharacters = function () {
    for (var i = 0; i < this._characterSprites.length; i++) {
        var sprite = this._characterSprites[i];
        if (!sprite.isTile()) sprite.show();
    }
};

//=============================================================================
// Spriteset_Battle
//=============================================================================

if (Yanfly.Param.ScaleBattleback) {

    if (Utils.RPGMAKER_VERSION && Utils.RPGMAKER_VERSION >= '1.3.2') {

        // Rewriting the battlebacks
        Spriteset_Battle.prototype.createBattleback = function () {
            this._back1Sprite = new Sprite_Battleback(this.battleback1Name(), 1);
            this._back2Sprite = new Sprite_Battleback(this.battleback2Name(), 2);
            this._battleField.addChild(this._back1Sprite);
            this._battleField.addChild(this._back2Sprite);
        };

        // No more updateBattleback
        Spriteset_Battle.prototype.updateBattleback = function () {
        };

    } else { // Version 1.3.0 and below

        Yanfly.Core.Spriteset_Battle_locateBattleback =
            Spriteset_Battle.prototype.locateBattleback;
        Spriteset_Battle.prototype.locateBattleback = function () {
            var sprite1 = this._back1Sprite;
            var sprite2 = this._back2Sprite;
            if (sprite1.bitmap.width <= 0) return;
            if (sprite2.bitmap.width <= 0) return;
            if (this._rescaledBattlebackSprite) return;
            this._rescaledBattlebackSprite = true;
            Yanfly.Core.Spriteset_Battle_locateBattleback.call(this);
            var height = this._battleField.height;
            sprite1.origin.y = sprite1.x + sprite1.bitmap.height - height;
            sprite2.origin.y = sprite1.y + sprite2.bitmap.height - height;
            this.rescaleBattlebacks();
        };

        Spriteset_Battle.prototype.rescaleBattlebacks = function () {
            this.rescaleBattlebackSprite(this._back1Sprite);
            this.rescaleBattlebackSprite(this._back2Sprite);
        };

        Spriteset_Battle.prototype.rescaleBattlebackSprite = function (sprite) {
            if (sprite.bitmap.width <= 0 || sprite.bitmap <= 0) return;
            var width = Graphics.boxWidth;
            var height = Graphics.boxHeight;
            var ratioX = width / sprite.bitmap.width;
            var ratioY = height / sprite.bitmap.height;
            if (ratioX > 1.0) {
                sprite.scale.x = ratioX;
                sprite.anchor.x = 0.5;
                sprite.x = width / 2;
            }
            if (ratioY > 1.0) {
                sprite.scale.y = ratioY;
                sprite.origin.y = 0;
                sprite.y = 0;
            }
        };

    } // Version 1.3.0 and below

} // Yanfly.Param.ScaleBattleback